﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Caching;

namespace IRC.IRepairClouds.DataLayer.Cache
{
    public static class CacheExtensions
    {
        public static string GetCacheKey(this EntityKey entityKey)
        {
            entityKey.ThrowIfNull("entityKey");
            if (entityKey.IsTemporary)
                throw new NotSupportedException();
            StringBuilder builder = new StringBuilder();
            builder.Append("EntitySet=").Append(entityKey.EntitySetName);
            foreach (EntityKeyMember member in entityKey.EntityKeyValues.OrderBy(i => i.Key, StringComparer.InvariantCulture)) {
                builder.Append(';');
                builder.Append(member.Key).Append("=").Append(member.Value);
            }
            return builder.ToString();
        }

        //public static Microsoft.Practices.EnterpriseLibrary.Caching.Cache GetRealCache(this ICacheManager cm)
        //{
        //    return (Microsoft.Practices.EnterpriseLibrary.Caching.Cache)typeof(Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager).GetField("realCache", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(cm);
        //}
    }
}
